package org.a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBC {
	public static void main(String[] args) {
		  Connection conn = getConn();
		   
		  
		    PreparedStatement pstmt=null;
		    try {
			
					String sql = " insert into users (id,name) values(?,?) ";

					pstmt = (PreparedStatement) conn.prepareStatement(sql);
					pstmt.setInt(1, 2);
					pstmt.setString(2, "jo");
					int i = pstmt.executeUpdate();
					System.out.println(" resutl: " + i);

				
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				
				if(pstmt!=null) {
					try {
						pstmt.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if(conn!=null) {
					try {
						conn.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		  
	}
	private static Connection getConn() {
	    String driver = "oracle.jdbc.driver.OracleDriver";
	    String url = "jdbc:oracle:thin:@172.16.39.205:1521:xe";
	    String username = "ab562";
	    String password = "aa412122";
	    Connection conn = null;
	    try {
	        Class.forName(driver); //classLoader,加载对应驱动
	        conn = (Connection) DriverManager.getConnection(url, username, password);
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return conn;
	}
}
